/**
 * 
 */
package com.hpxx.ylzswl.activity;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Request;


/**
 * 云码科技.All rights reserved
 * @ClassName FeedBackActivity
 * @TitleName FeedBackActivity
 * @author LuoXiang
 * @Data 2017-6-20 上午12:29:37
 */
public class FeedBackActivity extends BaseActivity implements
        ResultCallBack,View.OnClickListener {
	
	private EditText feedText;
	private EditText feedPhone;
	private TextView btn_feed_tj;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feed_back);
		back();
		setTitle("意见反馈");
	}
	
	@Override
	public void initView() {
		feedText = (EditText) findViewById(R.id.et_feed_text);
		feedText.setFilters(new InputFilter[]{emojiFilter});
		feedPhone = (EditText) findViewById(R.id.et_feed_phone);
		btn_feed_tj = (TextView) findViewById(R.id.btn_feed_tj);
		btn_feed_tj.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_feed_tj:
				post();
				break;

			default:
				break;
		}
	}
	

	private void post() {
		String text = feedText.getText().toString().trim();
		String phone = feedPhone.getText().toString().trim();
		
		
		
		if(TextUtils.isEmpty(text)){
			ToastUtil.showToast(this, "请输入您的意见");
			return;
		}
		
		if(text.length() < 20){
			ToastUtil.showToast(this, "反馈内容必须大于20个字符");
			return;
		}
		
		if(TextUtils.isEmpty(phone)){
			ToastUtil.showToast(this, "请输入您的手机号码");
			return;
		}
		
		if (!phone.matches(Constants.PHONE_ZZ)) {
			ToastUtil.showToast(this, "请输入有效的手机号");
			return;
		}
		
		OKHttpUtils.postAsync(this
				, HttpAddress.FEED_BACK_URL
				, new RequestParams(this).getUserFeedParams(phone,text,3)
				, this, true, 1);
		
	}

	@Override
	public void onSuccess(String response, int flag) {
		String status = JsonUtil.getString(response, "status", "");
		if("1".equals(status)){
			ToastUtil.showToast(this, "提交成功");
			finish();
		}else{
			String msg = JsonUtil.getString(response, "msg", "");
			ToastUtil.showToast(this, msg);
		}
	}

	@Override
	public void onFailure(Request request, IOException e, int flag) {
		
	}
	InputFilter emojiFilter = new InputFilter() {
		Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
				Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

		@Override
		public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
			Matcher emojiMatcher = emoji.matcher(source);
			if (emojiMatcher.find()) {

				return "";
			}
			return null;
		}
	};
}
